
*************************************************** 
** Stata code to prepare data to construct index **
***************************************************

import delimited using "\patent\LitigatedPatents.csv" , clear
keep patent masterindex class subclass claims patdef cafcera dcdecision dcstate ///
  district distdate affirmed acdecision acdate firstac dc2nddec dc2nddecdate ///
  ac2nddecision ac2nddecdate invstate assstate numinventors dcnumpatents ///
  acnumpatents issuedate  continuation division assignee

* 1. Date of decision at different courts 
replace distdate = "" if distdate == "unknown"
replace distdate = "1995" if  distdate == "//1995"
split(distdate), p(/)
rename distdate3 dcyear
destring dcyear, replace

rename distdate1 dcmonth
destring dcmonth, replace
*br dc* if dcmonth >= 12 & !mi(dcmonth)
replace dcyear = 1995 if dcmonth == 1995
replace dcmonth = . if dcmonth == 1995

rename distdate2 dcday
replace dcday = "" if dcday == " "
destring dcday, replace

gen dc = dcmonth if dcmonth > 12
replace dc = dc - 21916
format dc %td
replace dcyear = year(dc) if !mi(dc)
replace dcmonth = month(dc) if !mi(dc)
replace dcday = day(dc) if !mi(dc)
drop dc 

replace dc2nddecdate = "" if dc2nddecdate == "unknown"
replace dc2nddecdate = "" if dc2nddecdate == "n/a"
split(dc2nddecdate), p(/)

gen dc2 = dc2nddecdate1
destring dc2, replace
replace dc2 = . if dc2 < 10
replace dc2 = dc2 - 21916
format dc2  %td

gen dc2year = year(dc2)
gen dc2month = month(dc2)
gen dc2day = day(dc2)

destring dc2nddecdate*, replace
replace dc2year = dc2nddecdate3 if !mi(dc2nddecdate3)
replace dc2month = dc2nddecdate1 if !mi(dc2nddecdate1)
replace dc2day = dc2nddecdate2 if !mi(dc2nddecdate2)
drop dc2nddecdate1-dc2nddecdate3
drop dc2

replace acdate = "" if acdate == "n/a"
replace acdate = "" if acdate == "unknown"
destring acdate, replace
replace acdate = acdate - 21916
format acdate  %td
gen acyear = year(acdate)
gen acmonth = month(acdate)
gen acday = day(acdate)

replace ac2nddecdate = "" if ac2nddecdate == "n/a"
split(ac2nddecdate), p(/)
gen ac2 = ac2nddecdate1
destring ac2, replace
replace ac2 = . if ac2 < 10
replace ac2 = ac2 - 21916
format ac2 %td
gen ac2year = year(ac2)
gen ac2month = month(ac2)
gen ac2day = day(ac2)
destring ac2nddecdate*, replace
replace ac2year = ac2nddecdate3 if !mi(ac2nddecdate3)
replace ac2month = ac2nddecdate1 if !mi(ac2nddecdate1)
replace ac2day = ac2nddecdate2 if !mi(ac2nddecdate2)

replace issuedate = "" if issuedate == "unknow"   
split(issuedate), p(/)
destring issuedate3, replace
rename issuedate3 issue_year 
drop issuedat*

gen byte appeal = (affirmed != "n/a")
label var appeal "Appealed"

foreach var in valid ninf inv {
  gen byte `var'dc = 0  
  gen byte `var'ac = 0  
  label var `var'dc "`var' Dist Ct"
  label var `var'ac "`var' Appeals Ct"
}
 
*2. Outcome of decision at the district court
replace validdc = 1 if dcdecision == "V&I"  
replace ninfdc = 1 if dcdecision == "NINF"  
replace invdc = 1 if dcdecision == "INV" 

*3. Appeal Court vacated, reversed 
replace validac = 1 if inlist(acdecision, "rev(V&I)", ///
 "rev(Valid)", "~v&I", "~v&i")  & affirmed == "0"
replace validac = 1 if validdc == 1  & affirmed == "1"
replace validac = 1 if dcdecision != "V&I" & inlist(acdecision, "R&R" , ///
 "~R&R") & affirmed == "0" & dc2nddec == "n/a" &  ac2nddecision == "n/a"

replace ninfac = 1 if inlist(acdecision, "rev(NINF)", "ninf*", "~rev(NINF)") ///
 & affirmed == "0" 
replace ninfac = 1 if ninfdc == 1 & affirmed == "1"

replace invac = 1 if inlist(acdecision, "rev(INV)", "~rev(INV)") ///
  & affirmed == "0"
replace invac = 1 if dcdecision == "V&I" & inlist(acdecision, "R&R" , ///
 "~R&R") & affirmed == "0" & dc2nddec == "n/a" & dc2nddec == "n/a"
replace invac = 1 if invdc == 1 & affirmed == "1"
 
replace validac = 1 if inlist(dc2nddec, "V&I" , "~valid") ///
 & (inlist(ac2nddecision, "n/a", "affirmed"))
replace ninfac = 0 if dc2nddec == "V&I" & inlist(ac2nddecision, "n/a", "affirmed")
replace invac = 0 if dc2nddec == "V&I" & inlist(ac2nddecision, "n/a", "affirmed")
replace validac = 1 if inlist(ac2nddecision, "rev(V&I)", "rev(v&i)")

replace ninfac = 1 if dc2nddec == "NINF"   & inlist(ac2nddecision, "n/a", "affirmed")
replace validac = 0 if dc2nddec == "NINF"  & inlist(ac2nddecision, "n/a", "affirmed")
replace invac = 0 if dc2nddec == "NINF"  & inlist(ac2nddecision, "n/a", "affirmed")
replace ninfac = 1 if inlist(ac2nddecision, "rev(NINF)")

replace invac = 1 if inlist(dc2nddec, "INV" , "~invalid") ///
  & (inlist(ac2nddecision, "n/a", "affirmed"))
replace validac = 0 if dc2nddec == "INV" & inlist(ac2nddecision, "n/a", "affirmed")
replace ninfac = 0 if dc2nddec == "INV" & inlist(ac2nddecision, "n/a", "affirmed")
replace invac = 1 if inlist(ac2nddecision, "rev(INV)")

replace validac = . if appeal == 0
replace ninfac = . if  appeal == 0
replace invac = . if appeal == 0

replace claims = "" if real(claims) == .
destring(claims) , replace

gen classstr = class 
replace class = "" if real(class) == .
destring(class) , replace

gen year = dcyear 
replace year = dc2year if !mi(dc2year)
replace year = acyear if !mi(acyear)
replace year = ac2year if !mi(ac2year)
label var year "Final decision year"

gen byte cat = .
gen byte subcat = .
replace subcat = 11 if inlist(class, 8, 19, 71, 127, 442, 504)
replace subcat = 12 if inlist(class, 106, 118, 401, 427)
replace subcat = 13 if inlist(class, 48, 55, 95, 96)
replace subcat = 14 if inlist(class, 534, 536, 540, 544, 546, 548, ///
  549, 552, 554, 556, 558, 560, 562, 564, 568, 570)
replace subcat = 15 if inlist(class, 520, 521, 522, 523, 524, 525, ///
  526, 527, 528, 530)
replace subcat = 19 if inlist(class, 23, 34, 44, 102, 117, 149, 156, ///
  159, 162, 196, 201, 202, 203, 204, 205, 208, 210, 216, 222, ///
  252, 260, 261, 349, 366, 416, 422, 423, 430, 436, 494, 501, ///
  502, 510, 512, 516, 518, 585, 588)

replace subcat = 21 if inlist(class, 178, 333, 340, 342, 343, 358, ///
  367, 370, 375, 379, 385, 455)
replace subcat = 22 if inlist(class, 341, 380, 382, 395, 700, 701, ///
  702, 704, 705, 706, 707, 708, 709, 710, 712, 713, 714)
replace subcat = 23 if inlist(class, 345, 347)
replace subcat = 24 if inlist(class, 360, 365, 369, 711)

replace subcat = 31 if inlist(class, 424, 514)
replace subcat = 32 if inlist(class, 128, 600, 601, 602, 604, 606, 607)
replace subcat = 33 if inlist(class, 435, 800)
replace subcat = 39 if inlist(class, 351, 433, 623)

replace subcat = 41 if inlist(class, 174, 200, 327, 329, 330, 331, ///
  332, 334, 335, 336, 337, 338, 392, 439)
replace subcat = 42 if inlist(class, 313, 314, 315, 362, 372, 445)
replace subcat = 43 if inlist(class, 73, 324, 356, 374)
replace subcat = 44 if inlist(class, 250, 376, 378)
replace subcat = 45 if inlist(class, 60, 136, 290, 310, 318, 320, ///
  322, 323, 361, 363, 388, 429)
replace subcat = 46 if inlist(class, 257, 326, 438, 505)
replace subcat = 49 if inlist(class, 191, 218, 219, 307, 346, 348, ///
  377, 381, 386)
  
replace subcat = 51 if inlist(class, 65, 82, 83, 125, 141, 142, 144, ///
  173, 209, 221, 225, 226, 234, 241, 242, 264, 271, 407, 408, ///
  409, 414, 425, 451, 493)
replace subcat = 52 if inlist(class, 29, 72, 75, 76, 140, 147, 148, ///
  163, 164, 228, 266, 270, 413, 419, 420)
replace subcat = 53 if inlist(class, 91, 92, 123, 185, 188, 192, 251, ///
  303, 415, 417, 418, 464, 474, 475, 476, 477)
replace subcat = 54 if inlist(class, 352, 353, 355, 359, 396, 399)
replace subcat = 55 if inlist(class, 104, 105, 114, 152, 180, 187, ///
  213, 238, 244, 246, 258, 280, 293, 295, 296, 298, 301, 305, 410, 440)
replace subcat = 59 if inlist(class, 7, 16, 42, 49, 51, 74, 81, 86, 89, ///
  100, 124, 157, 184, 193, 194, 198, 212, 227, 235, 239, 254, ///
  267, 291, 294, 384, 400, 402, 406, 411, 453, 454, 470, 482, 483, 492, 508)

replace subcat = 61 if inlist(class, 43, 47, 56, 99, 111, 119, 131, ///
  426, 449, 452, 460)
replace subcat = 62 if inlist(class, 273, 446, 463, 472, 473)
replace subcat = 63 if inlist(class, 2, 12, 24, 26, 28, 36, 38, 57, 66, ///
  68, 69, 79, 87, 112, 139, 223, 450)
replace subcat = 64 if inlist(class, 37, 166, 171, 172, 175, 299, 405, 507)
replace subcat = 65 if inlist(class, 4, 5, 30, 70, 132, 182, 211, 256, ///
  297, 312)
replace subcat = 66 if inlist(class, 110, 122, 126, 165, 237, 373, 431, 432)
replace subcat = 67 if inlist(class, 138, 277, 285, 403)
replace subcat = 68 if inlist(class, 53, 206, 215, 217, 220, 224, 229, 232, 383)
replace subcat = 69 if inlist(class, 1, 14, 15, 27, 33, 40, 52, 54, 59, ///
  62, 63, 84, 101, 108, 109, 116, 134, 135, 137, 150, 160, 168, ///
  169, 177, 181, 186, 190, 199, 231, 236, 245, 248, 249, 269, 276, 278, 279, ///
  281, 283, 289, 292, 300, 368, 404, 412, 428, 434, 441, 462, 503) 

replace cat = int(subcat/10) 
drop class subclass
rename classstr class
label var cat "HJT tech category"
label var subcat "HJT tech subcategory"
label var class "Patent class"

tab cat , gen(hjt)
tab subcat , gen(hjtsub)

** Identify trial court state and circuit **
rename dcstate state_code
merge m:1 state_code year using "fedapp_yrs.dta"   
rename circuit dccircuit
rename state_code dcstate_code
drop if _merge == 2
drop _merge 

** Identify assignee state and circuit **
split assstate, p(;)
split assstate1, p(/)
drop assstate1
replace assstate2 = assstate12 if !mi(assstate12) & assstate12 != "a"
drop assstate12
rename assstate11 state_code 
replace state_code = "IL" if state_code == "Il"
merge m:1 state_code year using "fedapp_yrs.dta"   
drop if _m == 2 
drop _m 
rename state_code assstate1
rename circuit asscircuit1

gen state_code = subinstr(assstate2, " ", "", .)
drop assstate2
merge m:1 state_code year using "fedapp_yrs.dta"   
drop if _m == 2 
drop _m 
rename state_code assstate2	
rename circuit asscircuit2

gen state_code = invstate
merge m:1 state_code year using "fedapp_yrs.dta"   
drop if _merge == 2
drop _merge 
drop state_code 
rename circuit invcircuit

replace numinventor = "" if numinventor == "unknown"
destring numinventor, replace

replace continuation = "" if continuation == "unknown"
destring continuation, replace

gen decision_age = year - issue_year

compress
save "CAFC_cases_proed.dta" , replace
